# _*_ coding: utf-8 _*_
from odoo import models, fields, api
from odoo.exceptions import Warning


class ProductCustomerName(models.Model):
    _name = "product.customer.name"

    partner_id = fields.Many2one("res.partner", string="Customer", required=True,
                                 domain=[("parent_id", "=", False)])
    product_id = fields.Many2one("product.product", string="Product", required=True)
    product_customer_name = fields.Char(string="Product Customer Name")
    product_customer_code = fields.Char(string="Product Customer Code")

    @api.model
    def create(self, vals):
        partner_id = vals.get('partner_id')
        product_id = vals.get('product_id')
        pcn = self.env['product.customer.name'].search([('partner_id', '=', partner_id), ('product_id', '=', product_id)])
        if pcn:
            raise Warning(u'一个客户的一个产品只能设定一次')
        return super(ProductCustomerName, self).create(vals)
